// pages/index/list.js
const app = getApp(), util = require('../../utils/util.js');
let _this;
let pageNo = 1, pages = '', limit = 10;
Page({
  data: {
    //地址
    appLink: app.globalData.appLink, //app.globalData.appLink,
    // 链接判断
    buttonClicked: false,
    // nav
    navTitle: [],
    navChoose: 0,
    newList:[]
  },
  onLoad: function (options) {
    _this = this;
    _this.init(options);
  },
  // 初始化
  init: function (options) {
    if (options.navChoose != null || options.navChoose != '') {
      _this.getNavTab(options.navChoose);
    }
    _this.setData({
      navId: options.navId,
      newList: [],
      userInfo: app.globalData.programUserInfo
    })
    _this.list(1,10);
  },
  /**
   * nav
   */
  // 获取nav
  getNavTab: function (navChoose) {
    var pages = getCurrentPages(),
    curPage = pages[pages.length - 2];
    _this.setData({
      navTitle: curPage.data.navTitle,
      navChoose: navChoose
    })
  },
  navTarget: function (e) {
    var index = e.currentTarget.dataset.index,
      id = e.currentTarget.id;
    _this.setData({
      navChoose: index,
      navId: id,
      newList: []
    })
    _this.list(1, 10);
  },
  // 获取列表
  list: function (page, lim) {
    var data = {}, url = "salonObject";
    data.typeId = _this.data.navId;
    app.getList(page, lim, data, url, function (res) {
      if (res.data) {
        if (page == 1) {
          _this.setData({
            newList: [],
          })
        }
        pageNo = res.data.page / 1 + 1;
        pages = res.data.pages;
        var list = res.data.list;
        for (let i = 0; i < list.length; i++) {
          let newTime = new Date(),
            joinEndTime = new Date(list[i].joinEndTime);
          list[i].signUp = joinEndTime.getTime() > newTime.getTime() ? true : false
        }
        var newList = _this.data.newList;
        newList = newList.concat(list);
        
        _this.setData({
          newList: newList,
          pageNo: pageNo,
          pages: pages
        })
      }
    })
  },
  // 滚动触底
  scrolltolower: function () {
    // 判断是否是最后一页
    if (pages >= pageNo) {
      _this.list(pageNo, limit);
    }
  },
  // 去**页
  goNavTo: function (e) {
    app.goNavTo(e, _this.data.buttonClicked, _this);
  },
  // 返回上一页
  goBack:function(e){
    app.goBack(e, _this.data.buttonClicked, _this);
  }
})